<template>
	<view class="container"></view>
</template>

<script>
	import imMsg from "@/utils/common/message.js";
	export default {
		data() {
			return {
				redirectTo: null
			}
		},
		onLoad() {
			//拦截微信code
			if(this.$Route.query.code){
				this.redirectTo = this.$CHS.getRedirectTo()
				this.weixinLogin(this.$Route.query.code)
			}else{
				this.getWeixinCode()
			}
			uni.showLoading({
				mask: true,
				title: '登录中...'
			})
		},
		onUnload() {
			uni.hideLoading()
		},
		methods: {
			//微信登录
			weixinLogin(code) {
				this.$http.get({
					url: '/api/oauth/weChatLogin',
					data: {
						code
					},
					noLoading: true
				}).then((data) => {
					if(data.success && data.result){
						if(!data.result.userInfo.is_bind_phone){
							//跳转绑定手机
							this.$Router.replace({name: 'bindphone', params:{ oauthId: data.result.userInfo.oauthId}})
						}else{
							//保存用户信息
							this.$store.commit('SET_USERINFO', data.result)
							//跳转首页或来源页面
							setTimeout(()=>{
								//更新IM用户资料
								imMsg.login(data.result.userInfo).then((ready) => {
									//登录成功提示
									this.$ShowToast({text: '微信登录成功'})
									//查询优惠活动
									this.$CHS.getCouponActivity(data.result.userInfo.userId)
									//跳转网址
									if(this.redirectTo){
										this.$Router.replaceAll(this.redirectTo)
									}else{
										this.$Router.replaceAll({name: 'index'})
									}
									uni.removeStorageSync('user_phone')
								}).catch((err)=>{
									console.log(err);
									//登录提示
									this.$ShowToast({text: '微信登录失败，请重试', type: 2, time: 3})
									this.$store.commit('SET_USERINFO', {})
									this.$Router.replaceAll({name: 'userlogin', params: {random: Date.now()}})
								})
							}, 200);
						}
					}else{
						if(data.code == 10077 || data.code == 10006 ){
							uni.showModal({
								title: '系统提示',
								content: '当前微信绑定的手机号已注销，请重新申请注册',
								confirmText: '重新注册',
								success: (result) => {
									if (result.confirm) {
										this.$Router.replace({name: 'resetlogin', params: {phone: data.data}})
									} else {
										this.$Router.replaceAll({name: 'userlogin', params: {random: Date.now()}})
									}
								}
							})
						}else{
							uni.showModal({
								title: '系统提示',
								content: data.message,
								showCancel: false,
							})
						}
					}
				}).catch((err) => {
					console.log(err);
					uni.showToast({
						title: '登录失败，请稍后重试',
						icon: 'none'
					})
					//微信登录失败返回登录页面
					this.$Router.replaceAll({name: 'userlogin', params: {random: Date.now()}})
				})
			},
			//获取code
			getWeixinCode() {
				if(this.isWeixin()){
					//跳转打开code链接
					let redirect_url = window.location.href + '?wxcode=1'
					console.log(redirect_url);
					let scope = 'snsapi_userinfo'
					let url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+this.$CHS.config.wxAppId+"&redirect_uri="+ encodeURIComponent(redirect_url) + "&response_type=code&scope="+scope+"&state=wxlogin#wechat_redirect"
					uni.hideLoading()
					window.location.href = url
					//跳转打开code链接
				}else{
					uni.hideLoading()
					uni.showModal({
						title: '系统提示',
						content: '请先在微信客户端内打开当前页面，然后进行微信登录操作',
						showCancel: false,
						success: (res) => {
							if(res.confirm){
								this.$Router.replaceAll({name: 'userlogin', params: {random: Date.now()}})
							}
						}
					})
				}
			},
			//判断浏览器类型
			isWeixin() {
				let ua = navigator.userAgent.toLowerCase();
				if (ua.match(/micromessenger/i) == "micromessenger"){
					return true
				}else{
					return false
				}
			}
		}
	}
</script>

<style>

</style>
